/* 添加试卷参数模板 */
import React, { PureComponent } from 'react';
import { Form, Button, Input, Radio, InputNumber, Row, Col, message, Card } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import { HardLevelSelect } from '../Components/AntComponents'
import { addPaperTemplate } from '@/services/paperInfo'
import { isLianxi } from '@/utils/utils'
const layout = {
    labelCol: { span: 2 },
    wrapperCol: { span: 15 },
};
const tailLayout = {
    wrapperCol: { offset: 1 },
};
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props)
        this.state = {
            template_name: '',
            complexity: '0',
            total: 0,
            test_type: '2',
            multiple_num: 0,
            multiple_score: 0,
            multiple_deduct: 0,
            multiple_mistake: 0,
            choices_num: 0,
            choices_score: 0,
            choices_deduct: 0,
            choices_mistake: 0,
            judge_num: 0,
            judge_score: 0,
            judge_deduct: 0,
            judge_mistake: 0,
            test_time: 0,
            loading: false,
            badStart: 0,
            badEnd: 0,
            sosoStart: 0,
            sosoEnd: 0,
            goodStart: 0,
            goodEnd: 0,
            wonderfulStart: 0,
            wonderfulEnd: 0,
        }
    }
    changeWithTargetValue = (e: any, name: any) => {
        this.setState({
            [name]: e.target.value
        })
    }
    changeValue = (e: any, name: any) => {
        this.setState({
            [name]: e
        })
    }

    changeHardLevel = (e: any) => {
        this.setState({
            complexity: e
        })
    }

    submitHandle = () => {
        const { template_name,
            complexity,
            total,
            test_type,
            multiple_num,
            multiple_score,
            multiple_deduct,
            multiple_mistake,
            choices_num,
            choices_score,
            choices_deduct,
            choices_mistake,
            judge_num,
            judge_score,
            judge_deduct,
            judge_mistake,
            test_time, badStart, badEnd, sosoEnd, sosoStart,
            goodEnd, goodStart, wonderfulEnd, wonderfulStart } = this.state;
        // if (!template_name ||
        //     !complexity ||
        //     !total ||
        //     !test_type ||
        //     !multiple_num ||
        //     !multiple_score ||
        //     !choices_num ||
        //     !choices_score ||
        //     !judge_num ||
        //     !judge_score ||
        //     !test_time) return message.error('请完善模板信息')
        this.setState({
            loading: true
        })
        addPaperTemplate({
            template_name,
            complexity,
            total,
            test_type,
            multiple_num,
            multiple_score,
            multiple_deduct,
            multiple_mistake,
            choices_num,
            choices_score,
            choices_deduct,
            choices_mistake,
            judge_num,
            judge_score,
            judge_deduct,
            judge_mistake,
            test_time,
            fail: badStart + ',' + badEnd, pass: sosoStart + ',' + sosoEnd, good: goodStart + ',' + goodEnd,
            excellent: wonderfulStart + ',' + wonderfulEnd, 
        }).then(res => {
            this.setState({
                loading: false
            })
            if (res.status === 1) {
                message.success('提交成功')
                history.back()
            } else {
                message.error('提交失败')
            }
        })
    }
    backHandle = () => {
        history.back()
    }
    render() {
        const { template_name,
            complexity,
            total,
            test_type,
            multiple_num,
            multiple_score,
            multiple_deduct,
            multiple_mistake,
            choices_num,
            choices_score,
            choices_deduct,
            choices_mistake,
            judge_num,
            judge_score,
            judge_deduct,
            judge_mistake,
            test_time, loading,
            badStart, badEnd, sosoEnd, sosoStart,
            goodEnd, goodStart, wonderfulEnd, wonderfulStart
        } = this.state;
        const isTzzy:any = location.href.includes('Tz')
        return (
            <PageContainer title="添加试卷参数模板">
                <Card>
                    <Form>
                        <Form.Item {...layout} label="模板名称">
                            <Input style={{ width: 200 }} placeholder="请输入模板名称" onChange={(e) => this.changeWithTargetValue(e, 'template_name')}></Input>
                        </Form.Item>
                        <Form.Item {...layout} label="难易程度">
                            <HardLevelSelect onChange={this.changeHardLevel} value={complexity} />
                        </Form.Item>
                        <Form.Item {...layout} label="试卷总分">
                            <InputNumber placeholder="请输入试卷总分" style={{ width: 200 }} onChange={(e) => this.changeValue(e, 'total')} />
                        </Form.Item>
                        <Form.Item {...layout} label="试卷类别">
                            <Radio.Group onChange={(e) => this.changeWithTargetValue(e, 'test_type')} value={test_type}>
                                <Radio value="2">模拟考试试卷</Radio>
                                <Radio value="1">正式考试试卷</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item {...layout} label="试题分布">
                            <Row>
                                <Col style={{ marginBottom: 10 }} span={6}>单选题 <InputNumber value={multiple_num} onChange={(e) => this.changeValue(e, 'multiple_num')} /> 道</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={multiple_score} onChange={(e) => this.changeValue(e, 'multiple_score')} /> 分&nbsp;&nbsp;(共<b>{multiple_num * multiple_score}</b>分 )</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={multiple_deduct} onChange={(e) => this.changeValue(e, 'multiple_deduct')} /> 分</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={multiple_mistake} onChange={(e) => this.changeValue(e, 'multiple_mistake')} /> 分</Col>
                            </Row>
                            {
                                (!isLianxi()||!isTzzy())&& <Row>
                                <Col style={{ marginBottom: 10 }} span={6}>多选题 <InputNumber value={choices_num} onChange={(e) => this.changeValue(e, 'choices_num')} /> 道</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={choices_score} onChange={(e) => this.changeValue(e, 'choices_score')} /> 分&nbsp;&nbsp;(共<b>{choices_num * choices_score}</b>分 )</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={choices_deduct} onChange={(e) => this.changeValue(e, 'choices_deduct')} /> 分</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={choices_mistake} onChange={(e) => this.changeValue(e, 'choices_mistake')} /> 分</Col>
                            </Row>
                            }
                           
                            <Row>
                                <Col style={{ marginBottom: 10 }} span={6}>判断题 <InputNumber value={judge_num} onChange={(e) => this.changeValue(e, 'judge_num')} /> 道</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>每题 <InputNumber value={judge_score} onChange={(e) => this.changeValue(e, 'judge_score')} /> 分&nbsp;&nbsp;(共<b>{judge_num * judge_score}</b>分 )</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>未答扣 <InputNumber value={judge_deduct} onChange={(e) => this.changeValue(e, 'judge_deduct')} /> 分</Col>
                                <Col style={{ marginBottom: 10 }} span={6}>答错扣 <InputNumber value={judge_mistake} onChange={(e) => this.changeValue(e, 'judge_mistake')} /> 分</Col>
                            </Row>
                        </Form.Item>
                        <Form.Item {...layout} label="分值标准">
                            <Row>
                                <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>不及格</div> <InputNumber value={badStart} onChange={(e) => this.changeValue(e, 'badStart')} /> —— <InputNumber value={badEnd} onChange={(e) => this.changeValue(e, 'badEnd')} /> </Col>
                                <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>及格</div>  <InputNumber value={sosoStart} onChange={(e) => this.changeValue(e, 'sosoStart')} /> —— <InputNumber value={sosoEnd} onChange={(e) => this.changeValue(e, 'sosoEnd')} /></Col>
                                <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>良好</div>  <InputNumber value={goodStart} onChange={(e) => this.changeValue(e, 'goodStart')} /> —— <InputNumber value={goodEnd} onChange={(e) => this.changeValue(e, 'goodEnd')} /></Col>
                                <Col span={24} style={{ marginBottom: 10 }}><div style={{ width: 50, float: 'left' }}>优秀</div>  <InputNumber value={wonderfulStart} onChange={(e) => this.changeValue(e, 'wonderfulStart')} /> —— <InputNumber value={wonderfulEnd} onChange={(e) => this.changeValue(e, 'wonderfulEnd')} /></Col>
                            </Row>
                        </Form.Item>
                        <Form.Item {...layout} label="考试时长">
                            <InputNumber onChange={(e) => this.changeValue(e, 'test_time')} style={{ width: 200 }} /> 分钟
                        </Form.Item>
                        <Form.Item {...tailLayout}>
                            <Button type="primary" onClick={this.submitHandle} loading={loading}>提交</Button>
                            <Button onClick={this.backHandle} style={{ marginLeft: 25 }}>返回</Button>

                        </Form.Item>
                    </Form>
                </Card>
            </PageContainer>
        )
    }
}

